#include "main.h"

/*
*    数据包 6
*    包头*1+设备号*1+数据*2+和校验低八位+包尾 
*/
#define fifo_path   "/home/debian/qt_test/fifo"
#define fifo2_path   "/home/debian/qt_test/fifo2"

sem_t led_sem, beep_sem;

u_int8_t rx_data[6] = {0};
u_int8_t tx_data[8] = {0xa5, 0, 0, 0, 0, 0, 0, 0x5a};

int main(int argc, char *argv[])
{
    int ret;
    /********************************** to cread fifo **************************************/
    int fifo_fd, fifo2_fd;
    ret = access(fifo_path, F_OK);
    if( ret < 0 ){
        ret = mkfifo(fifo_path, O_RDWR);
        if( ret < 0 )
            printf("code FIFO file creat error!\n");
    }
    ret = access(fifo2_path, F_OK);
    if( ret < 0 ){
        ret = mkfifo(fifo_path, O_RDWR);
        if( ret < 0 )
            printf("2 FIFO2 file creat error!\n");
    }

    fifo_fd = open(fifo_path, O_RDWR);
    if( fifo_fd < 0 ){
        printf("code FIFO file open error!\n");
        exit(-1);
    }
    fifo2_fd = open(fifo2_path, O_RDWR);
    if( fifo2_fd < 0 ){
        printf("2 FIFO2 file open error!\n");
        exit(-1);
    }
    /**************************************************************************************/
    /********************************** to init sem ***************************************/
    ret = P_V_sem_init(NULL);
    if( ret < 0 )
        exit(-1);
    /**************************************************************************************/
    /******************************* to create pthread ************************************/
    ret = thread_create(NULL);
    if( ret < 0 )
        exit(-1);
    /**************************************************************************************/

    while(1){
        if( read(fifo_fd, rx_data, 6) > 0 ){
            //判断包头 包尾及其校验
            if( rx_data[0] == 0xa5 ){
                if( rx_data[5] == 0x5a ){
                    if( rx_data[4] == ((rx_data[1]+rx_data[2]+rx_data[3])&0xff) ){

                        switch (rx_data[1])
                        {
                        case 0x1:{
                            sem_post(&led_sem);
                            usleep(1*1000);
                            sem_wait(&led_sem);
                            break;
                        }
                        case 0x2:{
                            sem_post(&beep_sem);
                            usleep(1*1000);
                            sem_wait(&beep_sem);
                            break;
                        }
                        default:
                            break;
                        }
                    }
                }
            }
        }
        usleep(10*1000);
    }
    
    return 0;
}



int P_V_sem_init(void *arg)
{
    int ret;
    ret = sem_init(&led_sem, 0, 0);
    if( ret < 0 ){
        printf("led sem init error!\n");
        return -1;
    }
    ret = sem_init(&beep_sem, 0, 0);
    if( ret < 0 ){
        printf("beep sem init error!\n");
        return -1;
    }   
}
int thread_create(void *arg)
{
    int ret;
    pthread_t led_thread, beep_thread, adc_thread;
    ret = pthread_create(&led_thread, NULL, led_task, NULL);
    if( ret < 0 ){
        printf("led task create error!\n");
        return -1;
    }
    ret = pthread_create(&beep_thread, NULL, beep_task, NULL);
    if( ret < 0 ){
        printf("led task create error!\n");
        return -1;
    }
    ret = pthread_create(&adc_thread, NULL, adc_task, NULL);
    if( ret < 0 ){
        printf("adc task create error!\n");
        return -1;
    }
    return 0;
}

void *led_task(void *arg)
{
    Led_dev_init();
}
void *beep_task(void *arg)
{
    beep_dev_init();
}
void *adc_task(void *arg)
{
    adc_dev_init();
}
